package com.dz.scrm.utils;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.util.Log;

import com.dz.scrm.mvp.model.entity.CallInfo;

import java.util.ArrayList;
import java.util.List;

import androidx.core.app.ActivityCompat;

/**
 * @Description: 类作用描述
 * @Author: 黄海
 * @CreateDate: 2021-6-8 14:07
 * @UpdateUser: 黄海
 * @UpdateDate: 2021-6-8 14:07
 * @UpdateRemark: 更新说明
 */
public class CallInfoService {
    private static final String TAG = "CallInfoService";

    /**
     * @Date : 21-6-8 下午1:02
     * @Author : root
     * @Params : 填写参数信息
     * @Return : 填写返回值信息
     * @Description : 获取通话记录
     */
    public static List<CallInfo> getCallInfos(final Context context) {
        final List<CallInfo> newList = new ArrayList<>();


        ContentResolver resolver = context.getContentResolver();
        // Uri uri = CallLog.Calls.CONTENT_URI;
        Uri uri = Uri.parse("content://call_log/calls");
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context,
                    new String[]{Manifest.permission.READ_CALL_LOG}, 1000);

        }
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context,
                    new String[]{Manifest.permission.READ_CONTACTS}, 1000);

        }
//        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
//            Log.e(TAG, "没有权限");
//            return null;
//        }
        try {
            Cursor cursor = resolver.query(
                    uri, null, null, null,
                    CallLog.Calls.DATE + " desc");
            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.CACHED_NAME));
                String number = cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.NUMBER));
                int type = cursor.getInt(cursor
                        .getColumnIndex(CallLog.Calls.TYPE));
                long date = cursor.getLong(cursor
                        .getColumnIndex(CallLog.Calls.DATE));
                long duration = cursor.getLong(cursor
                        .getColumnIndex(CallLog.Calls.DURATION));
                String address = cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.GEOCODED_LOCATION));

                CallInfo record = new CallInfo(name, number, date, type, duration,address);
                Log.e(TAG,"address -> "+address);
                newList.add(record);
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newList;
    }
}
